package cn.edu.scnu.controller;

import cn.edu.scnu.entity.Favourite;
import cn.edu.scnu.entity.User;
import cn.edu.scnu.service.FavouriteService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class FavouriteController {
    @Resource
    private FavouriteService favouriteService;



    @RequestMapping("/favourite/showFavourite")
    public String showFavourite(Model model, HttpSession session) {
        User user = (User) session.getAttribute("userLogin");
        // 如果用户没有登录，则跳转到登录界面
        if (user== null) {
            return "redirect:/toLogin";
        }
        List<Favourite> favourites = favouriteService.showFavourite(user.getPhone());
        model.addAttribute("favourites", favourites);
        return "favourite";
    }

    @RequestMapping("/favourite/deleteFavourite")
    public String deleteFavourite(String phone, Integer movieId) {
        favouriteService.deleteFavorite(phone, movieId);
        return "redirect:/favourite/showFavourite";
    }
}
